#include"Log.h"
#include<iostream>
#include<thread>
std::string levelToString(int level)
{
    switch (level)
    {
    case DEBUG:
        return "debug";
    case INFO:
        return "info";
    case WARNING:
        return "warning";
    case ERROR:
        return "error";
    case FATAL:
        return "fatal";
    }
    return "none";
}

std::string GetTimeString()
{
    time_t cur_time = time(nullptr);
    struct tm *format_time = localtime(&cur_time);
    if (format_time == NULL)
    {
        return "None";
    }
    char time_buffer[1024];
    snprintf(time_buffer, sizeof(time_buffer), "%0d-%0d-%0d %0d:%0d:%0d",
             format_time->tm_year + 1900,
             format_time->tm_mon + 1,
             format_time->tm_mday,
             format_time->tm_hour,
             format_time->tm_min,
             format_time->tm_sec);
    return time_buffer;
}

void LogMessage(std::string filename, int line, int level, const char *format, ...)
{
    std::string levelstr = levelToString(level);
    std::string timestr = GetTimeString();
    // std::cout << levelstr << " : " << timestr << std::endl;
    pid_t selfid = getpid();
    
    // pid_t selfid = std::this_thread::get_id();

    char buffer[1024];
    va_list arg;
    va_start(arg,format);
    vsnprintf(buffer,sizeof(buffer),format,arg);
    va_end(arg);
    // std::cout << levelstr << " : " << timestr << filename << " : " << line << buffer << std::endl;

    std::cout <<"["<<timestr<<"]"
    <<"["<<levelstr<<"]"
    <<"[pid:"<<selfid<<"]"
    <<"["<<filename<<"]"
    <<"["<<line<<"]"
    <<buffer<<std::endl;
}